#! /bin/sh
# PCP QA Test No. 297
# exercise pmproxy and client reconnect
#
# Copyright (c) 2005 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

which pmdumptext >/dev/null 2>&1 || \
    _notrun "No pmdumptext binary installed"
which pmproxy >/dev/null 2>&1 || \
    _notrun "No pmproxy binary installed"

PMCDLOG=$PCP_LOG_DIR/pmcd/pmcd.log

pmlogger_do_restore=false
pmproxy_was_running=false
[ -f $PCP_RUN_DIR/pmproxy.pid ] && pmproxy_was_running=true
echo "pmproxy_was_running=$pmproxy_was_running" >>$seq_full

status=0	# success is the default!
signal=$PCP_BINADM_DIR/pmsignal
killpid=""
username=`id -u -n`
trap "_cleanup; $sudo rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_cleanup()
{
    # cannot be sure what state pmcd is in if we take a signal, so restart
    #
    _service pmcd start >>$seq_full 2>&1
    _restore_auto_restart pmcd
    _wait_for_pmcd

    if $pmlogger_do_restore
    then
	_service pmlogger start >>$seq_full 2>&1
	_restore_auto_restart pmlogger
	_wait_for_pmlogger
    fi

    $sudo $signal -a pmproxy >>$seq_full 2>&1
    [ -n "$killpid" ] && $signal $killpid
    if $pmproxy_was_running
    then
	echo "Restart pmproxy ..." >>$seq_full
	_service pmproxy restart >>$seq_full 2>&1
	_wait_for_pmproxy
    else
	echo "Stopping pmproxy ..." >>$seq_full
	_service pmproxy stop >>$seq_full 2>&1
    fi
}

_stop_auto_restart pmcd
_stop_auto_restart pmlogger
pmlogger_do_restore=true

realname=`hostname`
chopname=`echo $realname | sed -e 's/\..*//'`

# Note
# 	__pmDecodeError with error code > 0 is a notification
# 	from pmcd that the PMDA config has changed and is not
# 	relevant in the context of this test
_filter()
{
    sed \
	-e '/^pmWhichContext/d' \
	-e '/^pmUseContext/d' \
	-e '/^pmNewZone/d' \
	-e '/^pmUseZone/d' \
	-e '/^__pmFdLookup/d' \
	-e '/^__pmDecodeXtendError/d' \
	-e '/^__pmAddIPC/d' \
	-e '/^__pmSendCreds/d' \
	-e '/^IPC table/d' \
	-e '/^__pmConnectPMCD.* failed/d' \
	-e '/^__pmConnect(fd=[0-9][0-9]*(nsprFd=/d' \
	-e '/^__pmDecodeError: got error PDU (code=[1-9]/d' \
	-e '/^contexts\[/{
s/fd=[0-9][0-9]*/fd=N/
}' \
	-e '/^__pmConnectPMCD/{
s/'$realname'/MY_HOST/g
s/localhost\.localdomain/MY_HOST/g
s/localhost/MY_HOST/g
s/'$chopname'/MY_HOST/g
s/[0-9][0-9]*/N/g
}' \
	-e '/^pmReconnectContext.* failed/d' \
	-e '/^pmReconnectContext.* too soon/d' \
	-e '/^pmNewContext/{
s/'$realname'/MY_HOST/g
s/'$chopname'/MY_HOST/g
}' \
	-e '/^Context\[/{
s/'$realname'/MY_HOST/g
s/'$chopname'/MY_HOST/g
s/[0-9][0-9]*/N/g
}' \
	-e '/^[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/{
# pmdumptext
s//DATE/
s/	[0-9][0-9.]*/	N/g
}' \
    | _filter_pmproxy | uniq
}

_filter_pmproxy()
{
    sed -e '/^__pmSetSocketIPC: fd=/d' \
        -e '/^__pmSetDataIPC: fd=/d' \
        -e '/^__pmDataIPC: fd=/d' \
        -e '/^IPC table fd/d' \
	-e '/^__pmSetVersionIPC: fd=/d' \
	-e '/^__pmSetFeaturesIPC: fd=/d' \
	-e '/^__pmSquashTZ/d' \
	-e 's/localhost/MY_HOST/g' \
	-e '/^pmproxy: disabled time series, requires libuv support (missing)/d' \
    # end
}

if ! _service pmproxy stop >>$seq_full 2>&1; then _exit 1; fi
$sudo $signal -a pmproxy >>$seq_full 2>&1

OLD_PCP_RUN_DIR=$PCP_RUN_DIR
mkdir -p $tmp.rundir
export PCP_RUN_DIR=$tmp.rundir
proxyargs="-Dcontext -U $username -d"
$PCP_BINADM_DIR/pmproxy $proxyargs -l $tmp.log 2>&1 | _filter_pmproxy
_wait_for_pmproxy 44322 $tmp.log || _exit 1

# real QA test starts here
export PMPROXY_HOST=localhost
export PMCD_RECONNECT_TIMEOUT=1
pmdumptext -Dcontext -t 0.5 sample.bin >$tmp.out 2>&1 &
killpid=$!

# don't need to confuse pmproxy or pmcd ...
#
unset PMPROXY_HOST

sleep 3
echo "killing pmproxy ..."
$signal -a pmproxy >>$seq_full 2>&1
sleep 2
echo "1st pmproxy log" >$seq_full
cat $tmp.log >>$seq_full

echo "re-starting pmproxy ..."
$PCP_BINADM_DIR/pmproxy $proxyargs -l $tmp.log 2>&1 | _filter_pmproxy
_wait_for_pmproxy 44322 $tmp.log || _exit 1

sleep 4

primary_logger_pid=`$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger' | grep ' -P' | $PCP_AWK_PROG '{ print $2 }'`
if [ -z "$primary_logger_pid" ]
then
    echo "Argh! primary pmlogger not running?"
    exit
fi
echo "killing pmcd ..." | tee -a $seq_full
if ! _service pmlogger stop >>$seq_full 2>&1; then _exit 1; fi
_wait_pmlogger_end $primary_logger_pid || _exit 1
if ! _service pmcd stop >>$seq_full 2>&1; then _exit 1; fi
_wait_pmcd_end || _exit 1
echo >>$seq_full
echo "1st pmcd log" >>$seq_full
cat $PMCDLOG >>$seq_full

echo "re-starting pmcd ..." | tee -a $seq_full
export PCP_RUN_DIR=$OLD_PCP_RUN_DIR
if ! _service pmcd start >>$seq_full 2>&1; then _exit 1; fi
_restore_auto_restart pmcd
_wait_for_pmcd || _exit 1

# need to see the reconnect
sleep 2

echo >>$seq_full
echo "2nd pmproxy log" >>$seq_full
cat $tmp.log >>$seq_full
echo >>$seq_full
echo "2nd pmcd log" >>$seq_full
cat $PMCDLOG >>$seq_full

echo >>$seq_full
echo "pmdumptext output" >>$seq_full
cat $tmp.out >>$seq_full

_filter <$tmp.out

# success, all done
exit
